package com.dd.cloud.user.req.order;

import com.dd.cloud.user.entity.order.OrderFlow;
import com.dd.cloud.user.entity.product.PayParam;
import com.dd.cloud.common.utils.IPUtil;
import com.zk.pay.rqrs.AbstractRQ;
import com.zk.pay.rqrs.payorder.UnifiedOrderRQ;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * <p>
 * 服务预约
 * </p>
 *
 * @author czg
 * @since 2022-04-27
 */
@Data
public class CreateFlowReq implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 预约的排班id
     */
    @NotNull(message = "订单编号不能为空")
    private String orderNo;

    /**
     * 支付方式
     */
    private String payType;

    /**
     * price
     */
    @NotNull(message = "金额不能为空")
    private Integer price;

    /**
     * 收款的id, 机构或药房id
     */
    private Integer partyId;

    /**
     * 收款的类型 1机构 2药房
     */
    private Integer partyType;

    /**
     * openid
     */
    private String openid;

    public <T extends AbstractRQ> T params(HttpServletRequest request, PayParam payParam,
                                           OrderFlow orderFlow) {
        UnifiedOrderRQ rq = new UnifiedOrderRQ();
        rq.setWayCode(orderFlow.getPayment());
        rq.setChannelExtra(orderFlow.getOpenid());
        rq = rq.buildBizRQ();
        rq.setAppId(payParam.getAppId());
        rq.setMchOrderNo(orderFlow.getFlowNo());
        rq.setAmount(orderFlow.getTotalFee().longValue());
        rq.setVersion(payParam.getVersion());
        rq.setClientIp(IPUtil.getIpAddress(request));
        rq.setMchNo(payParam.getMchId());
        rq.setSubject("订单编号" + orderFlow.getOrderNo());
        rq.setBody(payParam.getBody());
//        if (rq.getWayCode().equals(CS.PAY_WAY_CODE.WX_JSAPI)){
//            rq = rq.buildBizRQ();
//            return (T) rq;
//        }
//        if (rq.getWayCode().equals(CS.PAY_WAY_CODE.ALI_JSAPI)){
//            AliJsapiOrderRQ arq = (AliJsapiOrderRQ) rq;
//            arq.setBuyerUserId(payReq.getOpenId());
//            return (T) arq;
//        }
        return (T) rq;
    }
}
